package com.colldown.iservice.demo.webc.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.colldown.common.A_Tools;
import com.colldown.common.Pager;
import com.colldown.iservice.demo.service.IDemoService;
import com.colldown.iservice.demo.webc.IDemoGrid;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

@Controller
@RequestMapping("/demo/grid")
public class DemoGridImpl implements IDemoGrid {

	@Autowired
	IDemoService demoService;
	
	@Override
	@RequestMapping("/query")
	public String query(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		Map map = new HashMap();
		map.put("demoName", "lucky luck!");
		//String result = this.demoService.queryDemo(map);
		model.addAttribute("sayhello","DemoGrid...");
		return "demo";
	}
	
	@RequestMapping("/queryList")
	public void queryList(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		//Map map = new HashMap();
		//map.put("demoName", "lucky luck!");
		//model.addAttribute("sayhello","DemoGrid...");
		 int iDisplayStart =A_Tools.NullToNumber(request.getParameter("iDisplayStart"));
		 int iDisplayLength = A_Tools.NullToNumber(request.getParameter("iDisplayLength"));
		 String sEcho = A_Tools.NullToStr(request.getParameter("sEcho"));
		 System.out.println(request.getParameter("aa")+"%%%%%% "+request.getParameter("bb"));
		// System.out.println(sEcho +" *** "+iDisplayLength);
		List list = new ArrayList();
        for(int i=0;i<5;i++){
        	Map map = new HashMap();
        	map.put("name", "Tiger Nixon"+i);
        	map.put("position", "System Architect"+i);
        	map.put("salary", "$320,800");
        	map.put("start_date", "2011-04-25");
        	map.put("office", "Edinburgh");
        	map.put("extn", "Edinburgh");
        	

        	list.add(map);
        }	
        Pager page = new Pager();
        page.setDataResult(list);
        page.setiTotalRecords(list.size());
        page.setiTotalDisplayRecords(list.size());
        page.setiDisplayLength(iDisplayLength);
        page.setsEcho(sEcho);
        Gson gson = new Gson();
		A_Tools.printJson(response, gson.toJson(page));
		
	}
	@RequestMapping("/charts")
	public void getChartsTest(HttpServletRequest request,
			HttpServletResponse response, Model model){
		Map<String,String> pramMap = new HashMap();
		pramMap.put("begindate", A_Tools.NullToStr(request.getParameter("begindate")));
		pramMap.put("enddate", A_Tools.NullToStr(request.getParameter("enddate")));
		List<Map> list = this.demoService.queryZxt(pramMap);
		Gson gson = new Gson();
		
		//往前台返回的数据
		JsonObject rstJson = new JsonObject();
		
		//图表x轴 显示信息
		TreeSet<String> cateSet = new TreeSet();
		//根据地址分折线
		Set<String> addrSet = new HashSet();
		for(Map map :list){
			cateSet.add("'"+A_Tools.NullToStr(map.get("CURDAY"))+"'");
			addrSet.add(A_Tools.NullToStr(map.get("TERMINAL_ADDRESS")));
		}
		rstJson.addProperty("categories", cateSet.toString());
		//图表数据字段series 显示信息 
		List seriesList = new ArrayList();
		
		for(String str:addrSet){
			//series中单条线的数据信息
			Map seriesMap = new HashMap();
			//设置seriesMap的name字段
			seriesMap.put("name", str);
			
			//定义list存储data字段数据
			List joDatalist = new ArrayList();
			for(Map map :list){
				if(str.equals(map.get("TERMINAL_ADDRESS"))){
					joDatalist.add(map.get("TIME_DIFF"));
				}
			}
			//设置seriesMap的data字段
			seriesMap.put("data", joDatalist);
			//放入series列表
			seriesList.add(gson.toJson(seriesMap));
		}
		rstJson.addProperty("series", seriesList.toString());
		/*
		String [] matching = new  String []{"1000A","1000B","1000C"};

		String result ="";
		result+="[{name: '1000A', data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]}, {name: '1000B',";
		result+="data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]}, {name: '1000C',data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]";
		result+="}]";
		
		JsonObject rt = new JsonObject();
		rt.addProperty("categories", "['03-12', '03-13', '03-14', '03-15', '03-16', '03-17','03-18', '03-19', '03-20', '03-21', '03-22', '03-23']");
		rt.addProperty("series", result);
		System.out.println(rt.toString());
		*/
		A_Tools.printJson(response,rstJson.toString());
	}


}
